package com.yonyougov.devops.cruxcisupport.user;

import com.yonyougov.devops.cruxcisupport.user.dto.controller.*;
import com.yonyougov.devops.cruxcisupport.user.vo.UserInfoVo;
import com.yonyougov.devops.cruxcisupport.user.vo.UserMailVo;
import com.yonyougov.devops.cruxcisupport.user.vo.UserPageLikeDto;
import com.yonyougov.devops.cruxcisupport.user.vo.UserPageLikeVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.api.enums.BaseErrorCode;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/v2/user")
public class UserController extends CrudController<UserEntity, BigInteger> {

    private final UserService userService;

    UserController(UserService userService) {
        this.userService = userService;
    }

    @Override
    public BaseService<UserEntity, BigInteger> getService() {
        return userService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "前端用户登录调用", notes = "用户登录")
    @PostMapping(value = "/u/login")
    public BaseResponseDto login(@RequestBody @Valid UserLoginRequestDto request) {
        //获取前端传递的参数
        @NotEmpty String userName = request.getUserName();
        @NotEmpty String password = request.getPassword();
        logger.info("method login(),params: username is : {};password is : {}", userName, password);

        Map<String, Object> data = userService.login(userName, password);

        if (null == data) {
            return BaseResponseDto.getFailInstance(BaseErrorCode.DATA_NULL);
        }
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.setData(data);
        return baseResponseDto;
    }

    @ApiOperation(value = "前端用户新增调用", notes = "新增用户")
    @PostMapping(value = "/u/add")
    @SneakyThrows
    public BaseResponseDto addUser(@RequestBody @Valid UserAddRequestDto request) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(request, userEntity);

        userService.save(userEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端用户删除调用", notes = "根据用户主键逻辑删除,根据用户id物理删除用户角色表")
    @PostMapping(value = "/u/delete/id")
    @SneakyThrows
    public BaseResponseDto deleteUserById(@RequestBody @Valid UserDeleteRequestDto request) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(request, userEntity);

        userService.deleteOneUser(userEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端用户修改调用", notes = "修改用户")
    @PostMapping(value = "/u/update")
    @SneakyThrows
    public BaseResponseDto updateUser(@RequestBody @Valid UserUpdateRequestDto request) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(request, userEntity);

        userService.save(userEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端用户列表调用", notes = "可选择根据用户名称,邮箱名称，进行有效用户的分页模糊查询")
    @PostMapping(value = "/u/page/like")
    public BaseResponseDto findByPageLike(@RequestBody @Valid UserPageLikeRequestDto request) {
        UserPageLikeDto userPageLikeDto = new UserPageLikeDto();
        BeanUtils.copyProperties(request, userPageLikeDto);

        Pagination<UserPageLikeVo> pagination = userService.findByPageLike(userPageLikeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "退出登录调用", notes = "退出登录")
    @PostMapping(value = "/u/logout")
    public BaseResponseDto logout(@RequestBody @Valid UserLogOutRequestDto request) {
        // 退出逻辑代码

        // 退出逻辑代码
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        return result;
    }

    @ApiOperation(value = "前端查询用户公钥调用", notes = "查询用户公钥")
    @GetMapping(value = "/u/publicKey/{userId}")
    public BaseResponseDto findPublicKeyByUserId(@PathVariable(value = "userId") BigInteger userId) {

        String key = userService.findPublicKeyByUserId(userId);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", key);
        return result;
    }

    @ApiOperation(value = "前端用户保存公钥调用", notes = "保存公钥")
    @PostMapping(value = "/u/publicKey")
    @SneakyThrows
    public BaseResponseDto updateUser(@RequestBody @Valid UserPublicKeyRequestDto request) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(request, userEntity);

        userService.updatePubKey(userEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端查询用户调用", notes = "查询所有用户")
    @PostMapping(value = "/u/all")
    public BaseResponseDto findAll() {

        UserEntity userEntity = UserEntity.builder().build();
        userEntity.setPageAble(false);
        List<UserEntity> allUser = userService.findAllBy(userEntity).getDatas();

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", allUser);
        return result;
    }

    @ApiOperation(value = "前端根据用户类型查询用户调用", notes = "根据用户类型查询用户")
    @PostMapping(value = "/u/type")
    public BaseResponseDto findUserListByType(@RequestBody @Valid UserTypeRequestDto requestDto) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(requestDto, userEntity);
        userEntity.setPageAble(false);

        List<UserEntity> data = userService.findAllBy(userEntity).getDatas();

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", data);
        return result;
    }

    @ApiOperation(value = "前端获取当前登录的用户的信息调用", notes = "获取当前登录的用户的信息")
    @PostMapping(value = "/u/userinfo")
    public BaseResponseDto findUser() {

        UserInfoVo userInfoRespVo = userService.findCurrentUserInfoVo();

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", userInfoRespVo);
        return result;
    }

    @ApiOperation(value = "邮箱列表", notes = "邮箱列表")
    @PostMapping(value = "/u/mail")
    public BaseResponseDto mailList() {

        List<UserMailVo> data = userService.mailList();

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", data);
        return baseResponseDto;
    }
    //<!--用户自定义代码结束-->
}
